<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="Java源码/设计模式/个人记录">
<meta property="og:type" content="website">
<meta property="og:title" content="arry&#39;s blog">
<meta property="og:url" content="https://arrycoder.gitee.io/index.html">
<meta property="og:site_name" content="arry&#39;s blog">
<meta property="og:description" content="Java源码/设计模式/个人记录">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="arry&#39;s blog">
<meta name="twitter:description" content="Java源码/设计模式/个人记录">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://arrycoder.gitee.io/">





  <title>arry's blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?97f9adfda8d04eb91dd054a98264d191";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">arry's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            Tags
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/elasticsearch/深入查询/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/elasticsearch/深入查询/" itemprop="url">深入查询</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-08-12T16:33:10+08:00">
                2020-08-12
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/es/" itemprop="url" rel="index">
                    <span itemprop="name">es</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>Lucene不支持以下这些查询：</p>
<ul>
<li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html" target="_blank" rel="noopener"><code>fuzzy</code></a></li>
<li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html" target="_blank" rel="noopener"><code>prefix</code></a></li>
<li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html" target="_blank" rel="noopener"><code>query_string</code></a></li>
<li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html" target="_blank" rel="noopener"><code>regexp</code></a></li>
<li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html" target="_blank" rel="noopener"><code>wildcard</code></a></li>
</ul>
<p>而es为了支持这些查询，可以在rewrite里写指定改写规则，来将原来的查询语句，映射为Lucene支持的查询语句。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/elasticsearch/基本概念/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/elasticsearch/基本概念/" itemprop="url">基本概念</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-08-12T11:01:33+08:00">
                2020-08-12
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/es/" itemprop="url" rel="index">
                    <span itemprop="name">es</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="名词解释"><a href="#名词解释" class="headerlink" title="名词解释"></a>名词解释</h2><ul>
<li><p>索引：elasticsearch索引有可能由多个Lucene索引组成（分片），一个索引5.0之前可以类比于数据库一样，但是在6.0移除了类型之后， 一个索引里最好只存放一种类型的数据</p>
</li>
<li><p>类型：文档的类型，好比数据库的表，每个类型都有不同的结构。在6.x版本里已经移除了类型</p>
</li>
<li><p>文档：相当于一条记录，一个文档是没有固定的格式的，如果在索引文档的时候不指定字段类型，es会自动指定字段类型，但是这种方式并不可靠。因此最好是手动指定文档的字段类型。</p>
</li>
<li><p>映射：映射就相当于字段类型，比如text，keyword，int等等。不同的字段类型，处理的方式也不同，比如text会进行分词，而keyword则不会。</p>
</li>
<li><p>节点：一个es服务，就相当于一个节点，多个节点组成一个集群，提供了高可用以及高效率。</p>
</li>
<li><p>分片：es里，一个lucene索引就相当于一个分片。在一个集群里，每个es索引都会有一个主分片和n个从分片。</p>
</li>
</ul>
<h3 id="why"><a href="#why" class="headerlink" title="why"></a>why</h3><ol>
<li><p>为什么文档没有固定格式，还需要我们去配置文档类型</p>
<p>在文档里字段未设置类型的时候，es会根据传入的值为该字段选择一个类型。</p>
<p>下面做个例子，在插入一个文档前的结构：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"customer"</span> : &#123;</span><br><span class="line">    <span class="attr">"aliases"</span> : &#123; &#125;,</span><br><span class="line">    <span class="attr">"mappings"</span> : &#123;</span><br><span class="line">      <span class="attr">"doc"</span> : &#123;</span><br><span class="line">        <span class="attr">"properties"</span> : &#123;</span><br><span class="line">          <span class="attr">"age"</span> : &#123;</span><br><span class="line">            <span class="attr">"type"</span> : <span class="string">"text"</span>,</span><br><span class="line">            <span class="attr">"fields"</span> : &#123;</span><br><span class="line">              <span class="attr">"keyword"</span> : &#123;</span><br><span class="line">                <span class="attr">"type"</span> : <span class="string">"keyword"</span>,</span><br><span class="line">                <span class="attr">"ignore_above"</span> : <span class="number">256</span></span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">"name"</span> : &#123;</span><br><span class="line">            <span class="attr">"type"</span> : <span class="string">"text"</span>,</span><br><span class="line">            <span class="attr">"fields"</span> : &#123;</span><br><span class="line">              <span class="attr">"keyword"</span> : &#123;</span><br><span class="line">                <span class="attr">"type"</span> : <span class="string">"keyword"</span>,</span><br><span class="line">                <span class="attr">"ignore_above"</span> : <span class="number">256</span></span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>这里更新id为1的文档，更新之后会发现，多了一个type为long的类型<br>   <figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl -X POST <span class="string">"0.0.0.0:9300/customer/doc/1/_update?pretty&amp;pretty"</span> -H <span class="string">'Content-Type: application/json'</span> -d<span class="string">'</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string">  "doc": &#123; "name": "Number", "order":1 &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">'</span></span><br></pre></td></tr></table></figure></p>
   <figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"customer"</span> : &#123;</span><br><span class="line">    <span class="attr">"aliases"</span> : &#123; &#125;,</span><br><span class="line">    <span class="attr">"mappings"</span> : &#123;</span><br><span class="line">      <span class="attr">"doc"</span> : &#123;</span><br><span class="line">        <span class="attr">"properties"</span> : &#123;</span><br><span class="line">          <span class="attr">"age"</span> : &#123;</span><br><span class="line">            <span class="attr">"type"</span> : <span class="string">"text"</span>,</span><br><span class="line">            <span class="attr">"fields"</span> : &#123;</span><br><span class="line">              <span class="attr">"keyword"</span> : &#123;</span><br><span class="line">                <span class="attr">"type"</span> : <span class="string">"keyword"</span>,</span><br><span class="line">                <span class="attr">"ignore_above"</span> : <span class="number">256</span></span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">"name"</span> : &#123;</span><br><span class="line">            <span class="attr">"type"</span> : <span class="string">"text"</span>,</span><br><span class="line">            <span class="attr">"fields"</span> : &#123;</span><br><span class="line">              <span class="attr">"keyword"</span> : &#123;</span><br><span class="line">                <span class="attr">"type"</span> : <span class="string">"keyword"</span>,</span><br><span class="line">                <span class="attr">"ignore_above"</span> : <span class="number">256</span></span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;,</span><br><span class="line">          <span class="attr">"order"</span> : &#123;</span><br><span class="line">            <span class="attr">"type"</span> : <span class="string">"long"</span></span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这个时候，如果我们往order字段里更新一个字符串类型的值就会报错。因此，最好是在对文档进行索引之前，预定义好文档的各个类型。</p>
<p><img src="/blog/elasticsearch/基本概念/image-20200812161405233.png" alt="image-20200812161405233"></p>
<ol start="2">
<li><p>在es索引5.x之前的版本里，使用类似 index-&gt;database，mapping-type-&gt;table的结构来存储数据，而6.x之后要移除类型</p>
<p>如果是一个单节点的es服务，那么一个es索引对应的就是一个lucene索引，不同类型的文档在lucene里都是按同样的结构存储的。因此，在两个文档存在同名而不同类型的字段的时候，也会发生上面的错误。所以移除了类型，现在所有类型都是<strong>_doc</strong>。</p>
</li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/elasticsearch/官方文档命令/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/elasticsearch/官方文档命令/" itemprop="url">了解一下</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-07-30T11:51:20+08:00">
                2020-07-30
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/es/" itemprop="url" rel="index">
                    <span itemprop="name">es</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h1><p>对比MySQL来说，文档document&lt;-&gt;行记录 row record，类型type&lt;-&gt;表 table，索引index&lt;-&gt; 数据库</p>
<h2 id="逻辑设计"><a href="#逻辑设计" class="headerlink" title="逻辑设计"></a>逻辑设计</h2><h3 id="文档（Document）"><a href="#文档（Document）" class="headerlink" title="文档（Document）"></a>文档（Document）</h3><p>文档是es中数据的最小单位，有几个重要的属性。</p>
<ol>
<li>一个文档同时包含字段名，和字段值</li>
<li>可以是层次型的，互相包含</li>
<li>文档并不依赖于事先定义的结构，可以随意新增和删除字段</li>
</ol>
<h3 id="类型（Mapping-types）"><a href="#类型（Mapping-types）" class="headerlink" title="类型（Mapping types）"></a>类型（Mapping types）</h3><p>类似于数据库中的表，用来保存文档的结构。虽然文档是无模式的，但是我们最好将一种业务类型的数据放到同一个类型中。如果一个文档包含了新的字段，es也会将猜测他的类型，比如7-&gt;长整型,然后将这个文档存放起来。但是 猜测有时候也会出错，如果先映射了a字段为长整型，这时候再新增字符串类型的，就会出错。</p>
<h3 id="索引-（Index）"><a href="#索引-（Index）" class="headerlink" title="索引 （Index）"></a>索引 （Index）</h3><p>索引是映射类型的容器，类似数据库。索引除了存储各个独立的类型，还存放着索引的设置，比如刷新时间。</p>
<h2 id="物理设计"><a href="#物理设计" class="headerlink" title="物理设计"></a>物理设计</h2><h3 id="节点"><a href="#节点" class="headerlink" title="节点"></a>节点</h3><p>一个es实例就是一个节点，多个节点可以组成一个集群。我们也可以在一个服务器上启动多个实例。多个节点组成的集群可以保证es服务的稳定性，同时每个主分片都有副分片，能一定程度上提供容错性。</p>
<h3 id="分片"><a href="#分片" class="headerlink" title="分片"></a>分片</h3><p>一份分片是Lucene的一个索引，一个倒排索引。默认包含了文档的原始内容，以及词条字典和词频。词条字典和词频用来帮助es快速定位文档。</p>
<p>分片可以是主分片和副分片，副本分片是主分片的完整副本。</p>
<hr>
<p>新建索引：curl -X PUT “0.0.0.0:9300/customer?pretty”<br>新增文档：curl -X PUT “0.0.0.0:9300/customer/doc/1?pretty” -H ‘Content-Type: application/json’ -d’<br>{<br>  “name”: “John Doe”<br>}<br>‘<br>查询文档：curl -X GET “0.0.0.0:9300/customer/doc/1?pretty”<br>删除索引：curl -X DELETE “0.0.0.0:9300/customer?pretty”</p>
<p>新增文档：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl -X PUT <span class="string">"0.0.0.0:9300/customer/doc/12/?pretty&amp;pretty"</span> -H <span class="string">'Content-Type: application/json'</span> -d<span class="string">'</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string">  "doc": &#123; "name": "chen", "order":3 &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">'</span></span><br></pre></td></tr></table></figure>

<p>更新文档：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">curl -X POST <span class="string">"0.0.0.0:9300/customer/doc/1/_update?pretty&amp;pretty"</span> -H <span class="string">'Content-Type: application/json'</span> -d<span class="string">'</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string">  "doc": &#123; "name": "Number", "order":1 &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">'</span></span><br></pre></td></tr></table></figure>

<p>导入json：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">curl -H <span class="string">"Content-Type: application/json"</span> -XPOST <span class="string">'0.0.0.0:9300/bank/account/_bulk?pretty&amp;refresh'</span> --data-binary <span class="string">"@querying.json"</span></span><br><span class="line">curl <span class="string">'0.0.0.0:9300/_cat/indices?v'</span></span><br></pre></td></tr></table></figure>

<h2 id="queryAPI"><a href="#queryAPI" class="headerlink" title="queryAPI"></a>queryAPI</h2><p>curl -X GET “0.0.0.0:9300/bank/_search?q=*&amp;sort=account_number:asc&amp;pretty&amp;pretty”</p>
<h3 id="Query-DSL"><a href="#Query-DSL" class="headerlink" title="Query DSL"></a>Query DSL</h3><p>DSL是一种json形式的query body</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">curl -X GET <span class="string">"0.0.0.0:9300/bank/_search?pretty"</span> -H <span class="string">'Content-Type: application/json'</span> -d<span class="string">'</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string">	"_source":["firstname", "age"],</span></span><br><span class="line"><span class="string">  "query": &#123;</span></span><br><span class="line"><span class="string">    "match_all":&#123;&#125;</span></span><br><span class="line"><span class="string">  &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br><span class="line"><span class="string">'</span></span><br></pre></td></tr></table></figure>

<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">curl -X GET <span class="string">"0.0.0.0:9300/_mget?pretty"</span> -H <span class="string">'Content-Type: application/json'</span> -d<span class="string">'</span></span><br><span class="line"><span class="string">&#123;</span></span><br><span class="line"><span class="string">  "docs": [</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">      "_index": "customer",</span></span><br><span class="line"><span class="string">      "_id": "1"</span></span><br><span class="line"><span class="string">    &#125;,</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">      "_index": "bank",</span></span><br><span class="line"><span class="string">      "_id": "2"</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">  ]</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>



<hr>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/zk/zab/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/zk/zab/" itemprop="url">zab协议</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-07-22T16:31:30+08:00">
                2020-07-22
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>选举：每个server都发起投票，当得到半数服务支持的时候，就会被选为准Leader。然后准leader会获取这半数followers的epochId，选择最大的一个进行+1，然后发送给所有followers，然后followers会返回ack消息，ack消息里会包含这个服务处理的最后一个事务请求以及历史事务集合。 当leader接收到来自半数以上服务器的ack之后，会从这里挑出一个follower，将他的事务集合当做初始化事务集合。这个followers的事务ID要&gt;其他followers，并且处理的最后一个事务id要&gt;其他followers。</p>
<p>初始化：当followers收到的时候，会判断epoch是否相等，不相等就直接进入下个循环，说明当前还在上一轮或者上上轮，无法参与本轮的操作。如果相等的话，就可以处理发来的初始化事务了。</p>
<p>广播：接收到请求的时候，会封装成proposal发给所有followers，当半数followers ack之后，就会再发个commit请求。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/Spring/AOP/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/Spring/AOP/" itemprop="url">AOP</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-07-14T17:08:16+08:00">
                2020-07-14
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Spring/" itemprop="url" rel="index">
                    <span itemprop="name">Spring</span>
                  </a>
                </span>

                
                
                  , 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/Spring/AOP/" itemprop="url" rel="index">
                    <span itemprop="name">AOP</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>面向切面编程，将横切关注点与业务代码分离，比如log，一些锁操作、全局exception处理，提高代码的模块化。</p>
<ul>
<li>JoinPoint：方法和切入点。调用前、调用后、抛异常后等等</li>
<li>PointCut：拦截连接点的条件定义</li>
<li>Aspect：PointCut和JoinPoint的结合</li>
<li>Advise：通知，即要执行的部分</li>
<li>weave：将advise连接到对象里，并生成代理</li>
<li>Advisor：增强器</li>
</ul>
<h2 id="AopNameSpaceHandler"><a href="#AopNameSpaceHandler" class="headerlink" title="AopNameSpaceHandler"></a>AopNameSpaceHandler</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AopNamespaceHandler</span> <span class="keyword">extends</span> <span class="title">NamespaceHandlerSupport</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * Register the &#123;<span class="doctag">@link</span> BeanDefinitionParser BeanDefinitionParsers&#125; for the</span></span><br><span class="line"><span class="comment">	 * '&#123;<span class="doctag">@code</span> config&#125;', '&#123;<span class="doctag">@code</span> spring-configured&#125;', '&#123;<span class="doctag">@code</span> aspectj-autoproxy&#125;'</span></span><br><span class="line"><span class="comment">	 * and '&#123;<span class="doctag">@code</span> scoped-proxy&#125;' tags.</span></span><br><span class="line"><span class="comment">	 */</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// In 2.0 XSD as well as in 2.1 XSD.</span></span><br><span class="line">		registerBeanDefinitionParser(<span class="string">"config"</span>, <span class="keyword">new</span> ConfigBeanDefinitionParser());</span><br><span class="line">		registerBeanDefinitionParser(<span class="string">"aspectj-autoproxy"</span>, <span class="keyword">new</span> AspectJAutoProxyBeanDefinitionParser());</span><br><span class="line">		registerBeanDefinitionDecorator(<span class="string">"scoped-proxy"</span>, <span class="keyword">new</span> ScopedProxyBeanDefinitionDecorator());</span><br><span class="line"></span><br><span class="line">		<span class="comment">// Only in 2.0 XSD: moved to context namespace as of 2.1</span></span><br><span class="line">		registerBeanDefinitionParser(<span class="string">"spring-configured"</span>, <span class="keyword">new</span> SpringConfiguredBeanDefinitionParser());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> BeanDefinition <span class="title">parse</span><span class="params">(Element element, ParserContext parserContext)</span> </span>&#123;</span><br><span class="line">		AopNamespaceUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(parserContext, element);</span><br><span class="line">		extendBeanDefinition(element, parserContext);</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<p>定义了解析开启Aop标签的BeanDefinitionParser，这里关注<strong>aspectj-autoproxy</strong>解析器。这里注册创建了AnnotationAwareAspectJAutoProxyCreator，是AOP主要的逻辑实现所在。</p>
<h3 id="注册or升级BeanDefinition"><a href="#注册or升级BeanDefinition" class="headerlink" title="注册or升级BeanDefinition"></a>注册or升级BeanDefinition</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> BeanDefinition <span class="title">registerOrEscalateApcAsRequired</span><span class="params">(Class&lt;?&gt; cls, BeanDefinitionRegistry registry, Object source)</span> </span>&#123;</span><br><span class="line">		Assert.notNull(registry, <span class="string">"BeanDefinitionRegistry must not be null"</span>);</span><br><span class="line">		<span class="keyword">if</span> (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) &#123;</span><br><span class="line">			BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);</span><br><span class="line">			<span class="keyword">if</span> (!cls.getName().equals(apcDefinition.getBeanClassName())) &#123;</span><br><span class="line">				<span class="keyword">int</span> currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());</span><br><span class="line">				<span class="keyword">int</span> requiredPriority = findPriorityForClass(cls);</span><br><span class="line">				<span class="keyword">if</span> (currentPriority &lt; requiredPriority) &#123;</span><br><span class="line">					apcDefinition.setBeanClassName(cls.getName());</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		RootBeanDefinition beanDefinition = <span class="keyword">new</span> RootBeanDefinition(cls);</span><br><span class="line">		beanDefinition.setSource(source);</span><br><span class="line">		beanDefinition.getPropertyValues().add(<span class="string">"order"</span>, Ordered.HIGHEST_PRECEDENCE);</span><br><span class="line">		beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);</span><br><span class="line">		registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);</span><br><span class="line">		<span class="keyword">return</span> beanDefinition;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h3 id="处理target-class和expose-proxy"><a href="#处理target-class和expose-proxy" class="headerlink" title="处理target-class和expose-proxy"></a>处理target-class和expose-proxy</h3><p>把proxy-target-class和expose-proxy放到beanDefinition的属性里。</p>
<ul>
<li>proxy-target-class：Spring默认是基于jdk的动态代理，jdk的动态代理是通过接口实现的，运行时生成接口的实现类来完成对目标的代理。CGLib则支持对类进行代理，通过生成子类的方式来实现。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">useClassProxyingIfNecessary</span><span class="params">(BeanDefinitionRegistry registry, Element sourceElement)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (sourceElement != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">boolean</span> proxyTargetClass = Boolean.parseBoolean(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE));</span><br><span class="line">			<span class="keyword">if</span> (proxyTargetClass) &#123;</span><br><span class="line">				AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">boolean</span> exposeProxy = Boolean.parseBoolean(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE));</span><br><span class="line">			<span class="keyword">if</span> (exposeProxy) &#123;</span><br><span class="line">				AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>

<h2 id="创建AOP代理"><a href="#创建AOP代理" class="headerlink" title="创建AOP代理"></a>创建AOP代理</h2>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/dubbo/自适应扩展/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/dubbo/自适应扩展/" itemprop="url">自适应扩展</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-07-08T14:10:35+08:00">
                2020-07-08
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/dubbo/" itemprop="url" rel="index">
                    <span itemprop="name">dubbo</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>Dubbo的很多拓展都是由SPI加载的，包括Protocal、Cluster、LoadBalance等等。而为了保证在接口调用的时候，通过URL来加载指定的类（比如LoadBalance策略配置），Dubbo提供了自适应扩展的机制来保证。</p>
<p>首先会为接口生成一个具有代理功能的代码，并通过Javasist或者jdk来编译这段代码，最后通过反射创建代理类。</p>
<p>Dubbo生成的代理类会在方法里去调用，去加载实际的类，并调用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.common.extensionloader.ext1.SimpleExt.class).getExtension(extName);</span><br></pre></td></tr></table></figure>

<p>@Adaptive 注解主要是加在方法上面的，加在方法上说明需要靠框架来生成代理功能的代码。</p>
<p>直接看一段最核心的代码，用来生成代理类代码的方法。</p>
<p>ExtensionLoader.createAdaptiveExtensionClassCode()</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> String <span class="title">createAdaptiveExtensionClassCode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        StringBuilder codeBuilder = <span class="keyword">new</span> StringBuilder();</span><br><span class="line">        Method[] methods = type.getMethods();</span><br><span class="line">        <span class="keyword">boolean</span> hasAdaptiveAnnotation = <span class="keyword">false</span>;</span><br><span class="line">  			<span class="comment">// 判断方法有没有加注解，没加就直接抛错了</span></span><br><span class="line">        <span class="keyword">for</span> (Method m : methods) &#123;</span><br><span class="line">            <span class="keyword">if</span> (m.isAnnotationPresent(Adaptive.class)) &#123;</span><br><span class="line">                hasAdaptiveAnnotation = <span class="keyword">true</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// no need to generate adaptive class since there's no adaptive method found.</span></span><br><span class="line">        <span class="keyword">if</span> (!hasAdaptiveAnnotation)</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"No adaptive method on extension "</span> + type.getName() + <span class="string">", refuse to create the adaptive class!"</span>);</span><br><span class="line">				<span class="comment">// package，在接口类的目录下</span></span><br><span class="line">        codeBuilder.append(<span class="string">"package "</span>).append(type.getPackage().getName()).append(<span class="string">";"</span>);</span><br><span class="line">  		 <span class="comment">// import extensionLoader</span></span><br><span class="line">        codeBuilder.append(<span class="string">"\nimport "</span>).append(ExtensionLoader.class.getName()).append(<span class="string">";"</span>);</span><br><span class="line">  <span class="comment">// 自适应类在接口后面加 $Adaptive</span></span><br><span class="line">        codeBuilder.append(<span class="string">"\npublic class "</span>).append(type.getSimpleName()).append(<span class="string">"$Adaptive"</span>).append(<span class="string">" implements "</span>).append(type.getCanonicalName()).append(<span class="string">" &#123;"</span>);</span><br><span class="line"><span class="comment">// 遍历方法生成</span></span><br><span class="line">        <span class="keyword">for</span> (Method method : methods) &#123;</span><br><span class="line">            Class&lt;?&gt; rt = method.getReturnType();</span><br><span class="line">            Class&lt;?&gt;[] pts = method.getParameterTypes();</span><br><span class="line">            Class&lt;?&gt;[] ets = method.getExceptionTypes();</span><br><span class="line"><span class="comment">// 如果不包含adaptive注解，调用直接报错</span></span><br><span class="line">            Adaptive adaptiveAnnotation = method.getAnnotation(Adaptive.class);</span><br><span class="line">            StringBuilder code = <span class="keyword">new</span> StringBuilder(<span class="number">512</span>);</span><br><span class="line">            <span class="keyword">if</span> (adaptiveAnnotation == <span class="keyword">null</span>) &#123;</span><br><span class="line">                code.append(<span class="string">"throw new UnsupportedOperationException(\"method "</span>)</span><br><span class="line">                        .append(method.toString()).append(<span class="string">" of interface "</span>)</span><br><span class="line">                        .append(type.getName()).append(<span class="string">" is not adaptive method!\");"</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">              <span class="comment">// 以下这段代码的主要流程就是</span></span><br><span class="line">              <span class="comment">// 1. 查找URL，如果参数里没有，就从参数的attribute里找。都没有就报错</span></span><br><span class="line">              <span class="comment">// 2. 从URL里获取参数名，如果没有配置，那么就根据类名获取，比如 SimpleExt-&gt; simple.ext</span></span><br><span class="line">              <span class="comment">// </span></span><br><span class="line">                <span class="keyword">int</span> urlTypeIndex = -<span class="number">1</span>;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; pts.length; ++i) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (pts[i].equals(URL.class)) &#123;</span><br><span class="line">                        urlTypeIndex = i;</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// found parameter in URL type</span></span><br><span class="line">                <span class="keyword">if</span> (urlTypeIndex != -<span class="number">1</span>) &#123;</span><br><span class="line">                    <span class="comment">// Null Point check</span></span><br><span class="line">                    String s = String.format(<span class="string">"\nif (arg%d == null) throw new IllegalArgumentException(\"url == null\");"</span>,</span><br><span class="line">                            urlTypeIndex);</span><br><span class="line">                    code.append(s);</span><br><span class="line"></span><br><span class="line">                    s = String.format(<span class="string">"\n%s url = arg%d;"</span>, URL.class.getName(), urlTypeIndex);</span><br><span class="line">                    code.append(s);</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// did not find parameter in URL type</span></span><br><span class="line">                <span class="keyword">else</span> &#123;</span><br><span class="line">                    String attribMethod = <span class="keyword">null</span>;</span><br><span class="line">    </span><br><span class="line">                    <span class="comment">// find URL getter method</span></span><br><span class="line">                    LBL_PTS:</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; pts.length; ++i) &#123;</span><br><span class="line">                        Method[] ms = pts[i].getMethods();</span><br><span class="line">                        <span class="keyword">for</span> (Method m : ms) &#123;</span><br><span class="line">                            String name = m.getName();</span><br><span class="line">                            <span class="keyword">if</span> ((name.startsWith(<span class="string">"get"</span>) || name.length() &gt; <span class="number">3</span>)</span><br><span class="line">                                    &amp;&amp; Modifier.isPublic(m.getModifiers())</span><br><span class="line">                                    &amp;&amp; !Modifier.isStatic(m.getModifiers())</span><br><span class="line">                                    &amp;&amp; m.getParameterTypes().length == <span class="number">0</span></span><br><span class="line">                                    &amp;&amp; m.getReturnType() == URL.class) &#123;</span><br><span class="line">                                urlTypeIndex = i;</span><br><span class="line">                                attribMethod = name;</span><br><span class="line">                                <span class="keyword">break</span> LBL_PTS;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="keyword">if</span> (attribMethod == <span class="keyword">null</span>) &#123;</span><br><span class="line">                        <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"fail to create adaptive class for interface "</span> + type.getName()</span><br><span class="line">                                + <span class="string">": not found url parameter or url attribute in parameters of method "</span> + method.getName());</span><br><span class="line">                    &#125;</span><br><span class="line">    </span><br><span class="line">                    <span class="comment">// Null point check</span></span><br><span class="line">                    String s = String.format(<span class="string">"\nif (arg%d == null) throw new IllegalArgumentException(\"%s argument == null\");"</span>,</span><br><span class="line">                            urlTypeIndex, pts[urlTypeIndex].getName());</span><br><span class="line">                    code.append(s);</span><br><span class="line">                    s = String.format(<span class="string">"\nif (arg%d.%s() == null) throw new IllegalArgumentException(\"%s argument %s() == null\");"</span>,</span><br><span class="line">                            urlTypeIndex, attribMethod, pts[urlTypeIndex].getName(), attribMethod);</span><br><span class="line">                    code.append(s);</span><br><span class="line">    </span><br><span class="line">                    s = String.format(<span class="string">"%s url = arg%d.%s();"</span>, URL.class.getName(), urlTypeIndex, attribMethod);</span><br><span class="line">                    code.append(s);</span><br><span class="line">                &#125;</span><br><span class="line">    </span><br><span class="line">                String[] value = adaptiveAnnotation.value();</span><br><span class="line">                <span class="comment">// value is not set, use the value generated from class name as the key</span></span><br><span class="line">              <span class="comment">// 2. 从URL里获取参数名，如果没有配置，那么就根据类名获取，比如 SimpleExt-&gt; simple.ext</span></span><br><span class="line">              <span class="comment">// 如果指定了 @Adaptive(&#123;"key1", "key2"&#125;)，那么value就是['key1', 'key2']。后面会根据这两个key从URL里获取值</span></span><br><span class="line">                <span class="keyword">if</span> (value.length == <span class="number">0</span>) &#123;</span><br><span class="line">                    <span class="keyword">char</span>[] charArray = type.getSimpleName().toCharArray();</span><br><span class="line">                    StringBuilder sb = <span class="keyword">new</span> StringBuilder(<span class="number">128</span>);</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; charArray.length; i++) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (Character.isUpperCase(charArray[i])) &#123;</span><br><span class="line">                            <span class="keyword">if</span> (i != <span class="number">0</span>) &#123;</span><br><span class="line">                                sb.append(<span class="string">"."</span>);</span><br><span class="line">                            &#125;</span><br><span class="line">                            sb.append(Character.toLowerCase(charArray[i]));</span><br><span class="line">                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                            sb.append(charArray[i]);</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    value = <span class="keyword">new</span> String[]&#123;sb.toString()&#125;;</span><br><span class="line">                &#125;</span><br><span class="line">    </span><br><span class="line">                <span class="keyword">boolean</span> hasInvocation = <span class="keyword">false</span>;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; pts.length; ++i) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (pts[i].getName().equals(<span class="string">"com.alibaba.dubbo.rpc.Invocation"</span>)) &#123;</span><br><span class="line">                        <span class="comment">// Null Point check</span></span><br><span class="line">                        String s = String.format(<span class="string">"\nif (arg%d == null) throw new IllegalArgumentException(\"invocation == null\");"</span>, i);</span><br><span class="line">                        code.append(s);</span><br><span class="line">                        s = String.format(<span class="string">"\nString methodName = arg%d.getMethodName();"</span>, i);</span><br><span class="line">                        code.append(s);</span><br><span class="line">                        hasInvocation = <span class="keyword">true</span>;</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">    						<span class="comment">// 生成 获取extension，并调用的代码</span></span><br><span class="line">                String defaultExtName = cachedDefaultName;</span><br><span class="line">                String getNameCode = <span class="keyword">null</span>;</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = value.length - <span class="number">1</span>; i &gt;= <span class="number">0</span>; --i) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (i == value.length - <span class="number">1</span>) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (<span class="keyword">null</span> != defaultExtName) &#123;</span><br><span class="line">                            <span class="keyword">if</span> (!<span class="string">"protocol"</span>.equals(value[i]))</span><br><span class="line">                                <span class="keyword">if</span> (hasInvocation)</span><br><span class="line">                                    getNameCode = String.format(<span class="string">"url.getMethodParameter(methodName, \"%s\", \"%s\")"</span>, value[i], defaultExtName);</span><br><span class="line">                                <span class="keyword">else</span></span><br><span class="line">                                    getNameCode = String.format(<span class="string">"url.getParameter(\"%s\", \"%s\")"</span>, value[i], defaultExtName);</span><br><span class="line">                            <span class="keyword">else</span></span><br><span class="line">                                getNameCode = String.format(<span class="string">"( url.getProtocol() == null ? \"%s\" : url.getProtocol() )"</span>, defaultExtName);</span><br><span class="line">                        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                            <span class="keyword">if</span> (!<span class="string">"protocol"</span>.equals(value[i]))</span><br><span class="line">                                <span class="keyword">if</span> (hasInvocation)</span><br><span class="line">                                    getNameCode = String.format(<span class="string">"url.getMethodParameter(methodName, \"%s\", \"%s\")"</span>, value[i], defaultExtName);</span><br><span class="line">                                <span class="keyword">else</span></span><br><span class="line">                                    getNameCode = String.format(<span class="string">"url.getParameter(\"%s\")"</span>, value[i]);</span><br><span class="line">                            <span class="keyword">else</span></span><br><span class="line">                                getNameCode = <span class="string">"url.getProtocol()"</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="keyword">if</span> (!<span class="string">"protocol"</span>.equals(value[i]))</span><br><span class="line">                            <span class="keyword">if</span> (hasInvocation)</span><br><span class="line">                                getNameCode = String.format(<span class="string">"url.getMethodParameter(methodName, \"%s\", \"%s\")"</span>, value[i], defaultExtName);</span><br><span class="line">                            <span class="keyword">else</span></span><br><span class="line">                                getNameCode = String.format(<span class="string">"url.getParameter(\"%s\", %s)"</span>, value[i], getNameCode);</span><br><span class="line">                        <span class="keyword">else</span></span><br><span class="line">                            getNameCode = String.format(<span class="string">"url.getProtocol() == null ? (%s) : url.getProtocol()"</span>, getNameCode);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">                code.append(<span class="string">"\nString extName = "</span>).append(getNameCode).append(<span class="string">";"</span>);</span><br><span class="line">                <span class="comment">// check extName == null?</span></span><br><span class="line">                String s = String.format(<span class="string">"\nif(extName == null) "</span> +</span><br><span class="line">                                <span class="string">"throw new IllegalStateException(\"Fail to get extension(%s) name from url(\" + url.toString() + \") use keys(%s)\");"</span>,</span><br><span class="line">                        type.getName(), Arrays.toString(value));</span><br><span class="line">                code.append(s);</span><br><span class="line">    </span><br><span class="line">                s = String.format(<span class="string">"\n%s extension = (%&lt;s)%s.getExtensionLoader(%s.class).getExtension(extName);"</span>,</span><br><span class="line">                        type.getName(), ExtensionLoader.class.getSimpleName(), type.getName());</span><br><span class="line">                code.append(s);</span><br><span class="line">    </span><br><span class="line">                <span class="comment">// return statement</span></span><br><span class="line">              <span class="comment">// 返回值</span></span><br><span class="line">                <span class="keyword">if</span> (!rt.equals(<span class="keyword">void</span>.class)) &#123;</span><br><span class="line">                    code.append(<span class="string">"\nreturn "</span>);</span><br><span class="line">                &#125;</span><br><span class="line">    </span><br><span class="line">                s = String.format(<span class="string">"extension.%s("</span>, method.getName());</span><br><span class="line">                code.append(s);</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; pts.length; i++) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (i != <span class="number">0</span>)</span><br><span class="line">                        code.append(<span class="string">", "</span>);</span><br><span class="line">                    code.append(<span class="string">"arg"</span>).append(i);</span><br><span class="line">                &#125;</span><br><span class="line">                code.append(<span class="string">");"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">    <span class="comment">// 把代码拼接回code里</span></span><br><span class="line">            codeBuilder.append(<span class="string">"\npublic "</span>).append(rt.getCanonicalName()).append(<span class="string">" "</span>).append(method.getName()).append(<span class="string">"("</span>);</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; pts.length; i++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (i &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                    codeBuilder.append(<span class="string">", "</span>);</span><br><span class="line">                &#125;</span><br><span class="line">                codeBuilder.append(pts[i].getCanonicalName());</span><br><span class="line">                codeBuilder.append(<span class="string">" "</span>);</span><br><span class="line">                codeBuilder.append(<span class="string">"arg"</span>).append(i);</span><br><span class="line">            &#125;</span><br><span class="line">            codeBuilder.append(<span class="string">")"</span>);</span><br><span class="line">            <span class="keyword">if</span> (ets.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                codeBuilder.append(<span class="string">" throws "</span>);</span><br><span class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; ets.length; i++) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (i &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                        codeBuilder.append(<span class="string">", "</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                    codeBuilder.append(ets[i].getCanonicalName());</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            codeBuilder.append(<span class="string">" &#123;"</span>);</span><br><span class="line">            codeBuilder.append(code.toString());</span><br><span class="line">            codeBuilder.append(<span class="string">"\n&#125;"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        codeBuilder.append(<span class="string">"\n&#125;"</span>);</span><br><span class="line">        <span class="keyword">if</span> (logger.isDebugEnabled()) &#123;</span><br><span class="line">            logger.debug(codeBuilder.toString());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> codeBuilder.toString();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/mysql/redo/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/mysql/redo/" itemprop="url">redo.log</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-06-24T16:08:17+08:00">
                2020-06-24
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/mysql/" itemprop="url" rel="index">
                    <span itemprop="name">mysql</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>为了保证持久性，那每次改动不仅要修改BufferPool中的记录，还需要记录到磁盘中，否则在脏页还未刷到磁盘的时候，系统就被关闭了，就会导致数据丢失。</p>
<p>而MySQL中要刷盘必须要整个页面刷到磁盘中，如果每次改动都需要刷一个页面，会浪费很多资源。因此，MySQL提供了redolog 来记录“改动”，而不是记录“记录”。这样，在系统发生异常的时候，就可以根据redolog，将为及时刷盘的数据刷新到数据库的文件中去。</p>
<ul>
<li>为了保证持久性</li>
<li>刷盘时机<ul>
<li>logbuffer空间不足时</li>
<li>后台线程不停</li>
<li>正常关闭系统时</li>
<li>事务提交时</li>
<li>…………</li>
</ul>
</li>
<li>Log Sequence Number</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/mysql/事务/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/mysql/事务/" itemprop="url">事务</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-06-24T14:54:36+08:00">
                2020-06-24
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/mysql/" itemprop="url" rel="index">
                    <span itemprop="name">mysql</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>ACID是数据的一组属性，分别为acid、consistency、isolation、durability。</p>
<ul>
<li><p>acid（原子性）</p>
<p>一个事务的操作，是一个原子的行为，不可分割。</p>
</li>
<li><p>consistency一致性</p>
<p>最终的结果符合现实约束。满足应用系统的既定规则，比如转账前后，AB账户的金额总额不变。</p>
</li>
<li><p>隔离性</p>
<p>事务之间是互相隔离的，不会互相影响</p>
</li>
<li><p>持久性</p>
<p>事务提交之后，结果将被保留。</p>
</li>
</ul>
<h2 id="多个事务引发的问题"><a href="#多个事务引发的问题" class="headerlink" title="多个事务引发的问题"></a>多个事务引发的问题</h2><ul>
<li>脏读：读到其他事务未提交的修改值</li>
<li>脏写：修改了其他事务修改过的值</li>
<li>幻读：相同的查询条件，读到了之前没读到的记录</li>
<li>不可重复读：</li>
</ul>
<h2 id="事务隔离级别"><a href="#事务隔离级别" class="headerlink" title="事务隔离级别"></a>事务隔离级别</h2><p>Read Uncommitted</p>
<p>Read Committed</p>
<p>Read Repeatable</p>
<p>Serialization</p>
<hr>
<p>MySQL为了支持并发事务，提供了MVCC实现了不同事务隔离级别下的需求。当不对并发事务做任何限制的时候，通常都会出现脏读、幻读、脏写、不可重复读这几个问题。</p>
<h2 id="MVCC"><a href="#MVCC" class="headerlink" title="MVCC"></a>MVCC</h2><p>多版本并发控制，数据通过trx_id和undolog形成一条版本链，根据不同隔离级别在不同的时机生成readView，并根据readVIew里的事务id来遍历版本链来找到满足当前事务的数据。</p>
<h2 id="ReadView"><a href="#ReadView" class="headerlink" title="ReadView"></a>ReadView</h2><ul>
<li>m_ids: 当前活跃的事务id</li>
<li>min_trx_id: 最小的事务id</li>
<li>max_trx_ids:分配给下一个事务的id</li>
<li>creator_trx_id: 生成该readview的事务id</li>
</ul>
<p>因为事务ID是递增分配的，因此，我们就可以根据readview的这些值与记录里的事务ID进行比较。</p>
<ol>
<li>当记录的事务id = creator_trx_id，说明是当前事务修改的记录，所以可以直接读出来</li>
<li>当记录的事务id &lt; min_trx_id，说明该记录已经提交，所以可以直接读出来</li>
<li>当记录的事务id &gt; max_trx_ids，说明该事务id是在生成readview之后提交的，所以不可读</li>
<li>当记录的事务id在m_ids里，说明还未提交，不可读，如果不在，说明已经提交，就可以访问</li>
</ol>
<h3 id="生成时机"><a href="#生成时机" class="headerlink" title="生成时机"></a>生成时机</h3><ul>
<li><p>Read Committed</p>
<p>在每次查询的时候生成一次ReadView，因此当其他事务提交修改之后，当前事务是可以读到改动的。无法避免脏读的情况。</p>
</li>
<li><p>Repeatable Read</p>
<p>在事务开启之后，第一次读取数据的时候生成Read View</p>
</li>
<li><p>Read Uncommitted</p>
<p>直接读取最新记录，不需要读版本链</p>
</li>
<li><p>Serializable</p>
<p>串行化读是根据锁实现的，所以也不读版本链</p>
</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/jvm/JIT/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/jvm/JIT/" itemprop="url">JIT</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-06-10T15:44:34+08:00">
                2020-06-10
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>JIT（just in time）</p>
<p>HotSpot JVM在工作的时候通常是解释器和编译器共同工作的。程序刚开始运行的时候使用解释器执行代码，随着程序运行，编译器将越来越多的字节码优化、编译成本地代码。</p>
<h2 id="解释器"><a href="#解释器" class="headerlink" title="解释器"></a>解释器</h2><p>简单来说，相当于在工作的时候直接执行字节码，每翻译一行便执行一行代码，因此执行效率比较低，但是响应速度比较快。</p>
<h2 id="编译器"><a href="#编译器" class="headerlink" title="编译器"></a>编译器</h2><p>JVM的编译器并不是指将Java代码编译成字节码的过程，而是指将字节码编译成本地代码的过程。HotSpot的编译器分为两种Client、Server。即时编译器需要占用程序运行时间，因为需要收集一部分VM的运行信息来决定优化策略。</p>
<p>Client: C1编译器</p>
<p>Server：C2编译器，启动的时候较慢。</p>
<h2 id="触发标准"><a href="#触发标准" class="headerlink" title="触发标准"></a>触发标准</h2><p>HotSpot为每个方法提供了两个计数器，方法调用计数器，和回边计数器</p>
<p>C1 默认1500 C2 10000</p>
<h3 id="方法调用计数器"><a href="#方法调用计数器" class="headerlink" title="方法调用计数器"></a>方法调用计数器</h3><p>方法调用到一定次数就会触发JIT编译。</p>
<h3 id="回边计数器"><a href="#回边计数器" class="headerlink" title="回边计数器"></a>回边计数器</h3><p>字节码向后流转即回边，用于统计循环体的代码执行次数。在循环周期较长的时候，会直接将循环体内的代码替换为本地机器码执行。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://arrycoder.gitee.io/blog/并发/并发容器/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="arry">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/uploads/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="arry's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/blog/并发/并发容器/" itemprop="url">并发容器</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-06-09T22:47:10+08:00">
                2020-06-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/并发/" itemprop="url" rel="index">
                    <span itemprop="name">并发</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="并发容器"><a href="#并发容器" class="headerlink" title="并发容器"></a>并发容器</h1><p>JUC-&gt; java.util.concurrent。</p>
<p>juc里提供了一系列线程安全的容器，这些容器的接口和我们常用的线程不安全的容器类接口类似。</p>
<h2 id="ArrayList-gt-CopyOnWriteArrayList"><a href="#ArrayList-gt-CopyOnWriteArrayList" class="headerlink" title="ArrayList -&gt; CopyOnWriteArrayList"></a>ArrayList -&gt; CopyOnWriteArrayList</h2><h3 id="CopyOnWrite"><a href="#CopyOnWrite" class="headerlink" title="CopyOnWrite"></a>CopyOnWrite</h3><p>写操作时复制一个数组，在新增的时候，复制出一个副本，在副本上进行操作，操作完之后再更新回原来的List。</p>
<p>读操作不加锁，写操作加锁。</p>
<p>更新并不是实时可见的，适用于读多写少的场景。</p>
<h2 id="HashSet，TreeSet-gt-CopyOnWriteArraySet-ConcurrentSkipListSet"><a href="#HashSet，TreeSet-gt-CopyOnWriteArraySet-ConcurrentSkipListSet" class="headerlink" title="HashSet，TreeSet -&gt; CopyOnWriteArraySet,ConcurrentSkipListSet"></a>HashSet，TreeSet -&gt; CopyOnWriteArraySet,ConcurrentSkipListSet</h2><h2 id="HashMap，TreeMap-gt-ConcurrentHashMap-ConcurrentSkipListMap"><a href="#HashMap，TreeMap-gt-ConcurrentHashMap-ConcurrentSkipListMap" class="headerlink" title="HashMap，TreeMap-&gt; ConcurrentHashMap,ConcurrentSkipListMap"></a>HashMap，TreeMap-&gt; ConcurrentHashMap,ConcurrentSkipListMap</h2>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/blog/page/2/">2</a><span class="space">&hellip;</span><a class="page-number" href="/blog/page/5/">5</a><a class="extend next" rel="next" href="/blog/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/blog/uploads/avatar.jpg" alt="arry">
            
              <p class="site-author-name" itemprop="name">arry</p>
              <p class="site-description motion-element" itemprop="description">Java源码/设计模式/个人记录</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">42</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">arry</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  

  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
